/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package org.amarena2d.examples.scenes.invader;

import com.android1.amarena2d.nodes.sprites.Sprite;

public class PlayerShip extends Sprite {

    public static final float VELOCITY = 100F;
    private int lastMoveDirection = Direction.None;


    public PlayerShip() {
        super("invader/ship.png", 480 / 2 - 32, 10);

        bounds().setRect(0, 400, 480 - 62, 0);
    }

    public void reset() {
        setXY(480 / 2 - 32, 10);
        stop();
    }

    public void finished() {
        move().with(0, 20).accelerate(0, 5, 5);
    }


    public void calcMove(float x) {
        if (x > getX()) {
            moveRight();
        } else if (x < getX()) {
            moveLeft();
        } else {
            stop();
        }
    }

    public void stop() {
        move().stop();
        lastMoveDirection = 0;
    }

    public void moveLeft() {
        if (lastMoveDirection != Direction.Left) {
            move().with(-VELOCITY, 0);
            lastMoveDirection = Direction.Left;
        }
    }

    public void moveRight() {
        if (lastMoveDirection != Direction.Right) {
            move().with(VELOCITY, 0);
            lastMoveDirection = Direction.Right;
        }
    }

    private static interface Direction {
        public static final int Left = -1;
        public static final int Right = 1;
        public static final int None = 0;
    }
}
